import { Component, OnInit, Input, NgZone } from '@angular/core';

import { first } from 'rxjs/operators';

import { AppGlobalService } from '../../shared/service/app-global.service';
import { EventService } from '../../shared/service/event.service';
import { HttpService } from '../../shared/service/http.service';

@Component({
  selector: 'app-system-news',
  templateUrl: './system-news.component.html',
  styleUrls: ['./system-news.component.scss'],
})
export class SystemNewsComponent implements OnInit {

  @Input() tagData: any;
  disabled = false;
  pageNo: any = 1;
  params: any;
  sessionId = localStorage.getItem('sessionId');
  constructor(
    private appGlobalService: AppGlobalService,
    private http: HttpService,
    private eventService: EventService) { }
  ngOnInit() {
    this.params = this.tagData.data.submit;
  }

  jumpToPage(item) {
    localStorage.setItem('needRefresh', 'false');
    localStorage.removeItem('needRefreshTab3');
    const key = localStorage.getItem('tab');
    this.eventService.eventEmit.emit("badgeForTab", { [key]: Number(item.badge) });
    item.badge = 0;
    const sessionId = localStorage.getItem('sessionId');
    const pageParams = {
      pageId: item.goTo.link.target,
      params: item.goTo.link.params,
      sessionId
    };

    this.appGlobalService.goToForward(pageParams);

    const subParams = item.goTo.submit;
    subParams.sessionId = sessionId;
    this.http.post('application/submit', subParams, res => {
      // tslint:disable-next-line:no-string-literal
      if (res['success']) {

      } else {
        // tslint:disable-next-line:no-string-literal
        this.appGlobalService.showToast(res['message'], 3000, 'top');
      }
    });
  }



  loadData(event) {
    this.params.sessionId = this.sessionId;
    // 请求下一个页面
    // this.pageNo = this.pageNo + 1;
    // 请求获取新数据
    this.http.post('application/submit', this.params, res => {
      // 请求数据成功
      // tslint:disable-next-line:no-string-literal
      if (res['success']) {
        // 追加返回数据
        // tslint:disable-next-line:no-string-literal
        if (Array.isArray(res['data']['data']) && res['data']['data'].length > 0) {
          // tslint:disable-next-line:no-string-literal
          setTimeout(() => {
            const newData = res['data']['data'].concat(this.tagData['data']['data']);
            this.tagData['data']['data'] = newData;
            event.target.complete();
          }, 1500);

          // 参数替换
          // tslint:disable-next-line:no-string-literal
          this.params = res['data'].submit;
        } else {
          this.disabled = true;
          event.target.complete();
          // 没有数据了
          // this.appGlobalService.showToast('没有数据了...', 1000, 'middle');
        }
      } else {
        // 请求失败，更新当前页数
        this.pageNo = this.pageNo - 1;
        // 请求数据失败
        // tslint:disable-next-line:no-string-literal
        this.appGlobalService.showToast(res['message'], 2000, 'middle');
        event.target.complete();
      }
    });
  }


}
